//
//  GJVideoPreviewView.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/9/16.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJVideoPreviewView.h"
#import <AVFoundation/AVFoundation.h>
@interface GJVideoPreviewView ()
// 摄像后的预览模块
@property (strong, nonatomic) NSURL *videoURL;                                           //视频文件地址
@property (strong, nonatomic) AVPlayerLayer *playerLayer;
@property (strong, nonatomic) AVPlayer *player;
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (assign, nonatomic) CGFloat currentVideoTimeLength;                             //当前小视频总时长
@end

@implementation GJVideoPreviewView

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    
  }
  return self;
}

/**
 *  预览录制的视频
 */
- (void)previewVideoAfterShoot
{
  
  AVURLAsset *asset = [AVURLAsset assetWithURL:self.videoURL];
  
  //获取视频总时长
  Float64 duration = CMTimeGetSeconds(asset.duration);
  
  self.currentVideoTimeLength = duration;
  
  self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
  
  [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
  
  //  // 开始播放
  [self.player play];
  
  [self addObserverForPlayer];
  
  
}

- (void)addObserverForPlayer{
  @weakify(self);
  __block id obsever = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1, NSEC_PER_SEC)
                                            queue:NULL
                                       usingBlock:^(CMTime time) {
    @strongify(self);
                                           //进度 当前时间/总时间
                                           CGFloat progress = CMTimeGetSeconds(self.player.currentItem.currentTime) / CMTimeGetSeconds(self.player.currentItem.duration);
//在这里截取播放进度并处理
                                           if (progress == 1.0f) {
                                             //播放百分比为1表示已经播放完毕
                                             [self.player removeTimeObserver:obsever];
                                             //回调
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                               [GJUtil sendNotificationForRNEventEmitterWithActionKey:@"VIDEO_PLAY_STATE" parameter:@{@"state":@"3"}];
                                             });
                                           }

                                       }];
}


- (void)pause{
  [self.player pause];
}
- (void)playWithPath:(NSString *)path{
  if ([path gj_hasValue]) {
    [self.playerLayer removeFromSuperlayer];
    
    // 初始化AVPlayer
    self.player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:@""]];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = self.bounds;
    
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [self.layer addSublayer:self.playerLayer];
//    [self.layer insertSublayer:self.playerLayer atIndex:0];
    
    self.videoURL = [NSURL URLWithString:path];
    [self previewVideoAfterShoot];
    
    
    
  }
}
- (void)resume{
  [self.player pause];
  [self.player seekToTime:CMTimeMake(0,1)];
}

@end
